home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / blit / sym.c < prev   
Encoding:
C/C++ Source or Header  |  1989-01-28  |  5.0 KB  |  164 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: sym.c,v 4.1 88/06/21 13:19:15 bianchi Exp $
  9.     $Source: /tmp/mgrsrc/src/blit/RCS/sym.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/src/blit/RCS/sym.c,v $$Revision: 4.1 $";
  12.  
  13. /* do symbolic substitution for asm decls SUN version */
  14.  
  15. /*    sample stab entries
  16.     .stabs    "x:r(0,1)",0x40,0,4,7
  17.     .stabs    "i:r(0,1)",0x40,0,4,6
  18.     .stabs    "d:r(0,13)",0x40,0,1,12
  19.     .stabs    "s:r(0,13)=*(0,2)",0x40,0,1,13
  20.    sample sun instruction
  21.        bfins.l %d1, (%$dbase)[%$dst:%$field]
  22. */
  23.  
  24. #include "stdio.h"
  25. #include "hash.h"
  26.  
  27. #define GET_OPT(i)   \
  28.    strlen(argv[i])>2 ? argv[i]+2 : argv[++i]
  29.  
  30. #define FMT    "    .stabs    \"%[^:]:r%*[^\"]\",0x%d,%*d,%*d,%s"
  31. #define TSIZE        501            /* # of entries in hash table */
  32. #define KEY    "#$ %s %s"        /* key work entry */
  33. #define dprintf    if(debug)fprintf
  34.  
  35. char line[512];            /* input line buffer */
  36. char name[32];                /* parameter name */
  37. char reg[10];                /* register value */
  38.  
  39. struct table_entry *table[TSIZE];
  40.  
  41. main(argc,argv)
  42. int argc;
  43. char **argv;
  44.    {
  45.    int class;        /* storage class */
  46.    int n;
  47.    char *start, *end;    /* for finding sub's. */
  48.    char *index(), *get_end();
  49.    int value;        /* register value to substitute */
  50.    int count = 0;    /* # of subs */
  51.    int line_cnt=0;        /* input line # */
  52.    char type;        /* register type 'a' or 'd' */
  53.    int debug = 0;
  54.    register int i;
  55.  
  56.    for(i=1;i<argc;i++) {
  57.       if (*argv[i] == '-')
  58.          switch (argv[i][1]) {
  59.             case 'd':            /* turn on debugging */
  60.                debug++;
  61.                break;
  62.             case 'K':            /* add keyword */
  63.                   { 
  64.                   char *key = (GET_OPT(i));
  65.                   char *reg = argv[++i];
  66.                   add_entry(table,TSIZE,key);
  67.                   put_entry(table,TSIZE,key,reg);
  68.                   dprintf(stderr,"adding %s => %s from command line\n",key,reg);
  69.                   }
  70.                break;
  71.             default:
  72.                fprintf(stderr,"%s: bad flag %c ignored\n",argv[0],argv[i][1]);
  73.             }
  74.          }
  75.  
  76.    dprintf(stderr,"Asm symbolic processor\n");
  77.    while(gets(line) != NULL) {
  78.       line_cnt++;
  79.         n = sscanf(line,FMT,name,&class,reg);
  80.         if (n==3 && class == 40) {                    /* declaration */
  81.          if (is_entry(table,TSIZE,reg))  {        /* delete old name */
  82.             dlt_entry(table,TSIZE,get_entry(table,TSIZE,reg));
  83.             }
  84.          add_entry(table,TSIZE,reg);
  85.          add_entry(table,TSIZE,name);
  86.          put_entry(table,TSIZE,name,reg);
  87.          put_entry(table,TSIZE,reg,name);
  88.          puts(line);
  89.          dprintf(stderr,"adding %s => %s from stab entry\n",name,reg);
  90.          }
  91.       else if (sscanf(line,KEY,name,reg) == 2) {    /* keyword substitution */
  92.          add_entry(table,TSIZE,name);
  93.          put_entry(table,TSIZE,name,reg);
  94.          dprintf(stderr,"adding %s => %s from file\n",name,reg);
  95.          }
  96.       else if (start = index(line,'$')) {                /* parameter substitution */
  97.          fwrite(line,1,start-line,stdout);
  98.          do {
  99.             end = get_end(start+1);
  100.             strncpy(name,start+1,end-start-1);
  101.             name[end-start-1] = '\0';
  102.             if (is_entry(table,TSIZE,name)) { /* do substitution */
  103.                value = atoi(get_entry(table,TSIZE,name));
  104.                if (value > 7) {
  105.                   type = 'a';
  106.                   value -=8;
  107.                   }
  108.                else
  109.                   type = 'd';
  110.                printf("%c%d",type,value);
  111.                dprintf(stderr,"line %d: substituting <%c%d> for <%s>\n",
  112.                        line_cnt,type,value,name);
  113.                count++;
  114.                }
  115.             else if (is_reg(name))    {                            /* can't find sub */
  116.                printf("%s",name);
  117.                } 
  118.             else {            
  119.                printf("$%s",name);
  120.                fprintf(stderr,"%s: Line %d no register found for %s\n",
  121.                        *argv,line_cnt,name);
  122.                }
  123.             if (start = index(end,'$')) 
  124.                fwrite(end,1,start-end,stdout);
  125.             else
  126.                printf("%s\n",end);
  127.             }
  128.          while(start);
  129.          }
  130.       else                                                            /* pass through */
  131.          puts(line);
  132.       }
  133.    dprintf(stderr,"%d substitutions made on %d lines\n",count,line_cnt);
  134.    exit(0);
  135.    }
  136.  
  137. /* find end of word */
  138.  
  139. char *
  140. get_end(str)
  141. char *str;
  142.    {
  143.    register char c;
  144.    register int i=0;
  145.  
  146.    while (i++,(c = *str++)) {
  147.       if (c>='a' && c<='z') continue;
  148.       if (c>='A' && c <= 'Z') continue;
  149.       if (i>1 && c>='0' && c <= '9') continue;
  150.       if (c != '_') break;
  151.       }
  152.    return(str-1);
  153.    }
  154.  
  155. /* see if variable is [ad][0-7] */
  156.  
  157. int
  158. is_reg(name)
  159. char *name;
  160.    {
  161.    return( (name[0]=='d' || name[0] == 'a') &&
  162.            (name[1] >= '0' && name[1] <= '7'));
  163.    }
  164.